//
//  ViewController.m
//  PetAPP
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 Yuming. All rights reserved.
//

#import "ViewController.h"
#import "ViewSetUp.h"



@interface ViewController ()

@property (nonatomic, retain) CameraView *viewOfCamera;

@property (nonatomic, retain) HomeView *viewOfHome;

@property (nonatomic, retain) ShopView *viewOfShop;

@property (nonatomic, retain) ServerView *viewOfServer;

@property (nonatomic, retain) MyView *viewOfMy;

@property (nonatomic, assign) BOOL flag;

@end
/** 该VC来控制所有页面的切换 */
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createSubViews];
    [self createTabBarView];
    /** 更改navigation上面的内容 */
    changeHome(self);
    createGrayLine(self);
    [self handleNotificationCenter];
    /** 设置不自动设置内置insert属性，否则tableView位置不对 */
    self.automaticallyAdjustsScrollViewInsets = NO;

    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIView animateWithDuration:1 animations:^{
        self.redLine.alpha = 1;
    }];
    
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"timeStart" object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [UIView animateWithDuration:0.5 animations:^{
        self.redLine.alpha = 0;
    }];
    
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"timeStop" object:nil];
}

#pragma mark - navigationItem上面左侧和右侧按钮点击事件
- (void)handleAction:(UIButton *)button {
    NSLog(@"%ld",button.tag);
}
- (void)handleActionWithTap:(UITapGestureRecognizer *)tap {

    NSLog(@"%@",tap);

}
#pragma mark - 通过消息中心推出item的详情页面
- (void)handleNotificationCenter {
    [[NSNotificationCenter defaultCenter] addObserverForName:@"PushVC" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
   
        DetailOfPageViewController *detailVC = [[DetailOfPageViewController alloc] init];
        [self.navigationController pushViewController:detailVC animated:YES];
        [detailVC release];
        detailVC.URLdic = note.userInfo;
        detailVC.headerMark = note.object;

    }];
    [[NSNotificationCenter defaultCenter] addObserverForName:@"PushLVView" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        if ([note.object isEqualToString:@"1"]) {
            BeLostViewController *belostVC = [[BeLostViewController alloc] init];
            [self.navigationController pushViewController:belostVC animated:YES];
            [belostVC release];
            belostVC.URLDic = note.userInfo;
            
            
        }else if ([note.object isEqualToString:@"2"]) {
            ActivityViewController *activityVC = [[ActivityViewController alloc] init];
            [self.navigationController pushViewController:activityVC animated:YES];
            [activityVC release];
            activityVC.URLDic = note.userInfo;
        
        }else {
            FindViewController *findVC = [[FindViewController alloc] init];
            [self.navigationController pushViewController:findVC animated:YES];
            [findVC release];
            findVC.URLDic = note.userInfo;
        
        }
        
        
        
    }];

}






#pragma mark - 创建tabBar显示的视图
- (void)createSubViews {
    /** 创建shopView */
    self.viewOfShop = [[ShopView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 - 50)];
    [self.view addSubview:self.viewOfShop];
    [_viewOfShop release];
    
    /** 创建serverView */
    self.viewOfServer = [[ServerView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 - 50)];
    [self.view addSubview:self.viewOfServer];
    [_viewOfServer release];
    
    /** 创建myView */
    self.viewOfMy = [[MyView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 - 50)];
    [self.view addSubview:self.viewOfMy];
    [_viewOfMy release];
    
    /** 创建homeView */
    self.viewOfHome = [[HomeView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 - 50)];
    [self.view addSubview:self.viewOfHome];
    [_viewOfHome release];
    
#pragma mark ** 实现block方法更改contentOffset属性
    [self.viewOfHome changePageScrollWith:^(CGFloat width, CGFloat index) {
        self.redLine.transform = CGAffineTransformMakeTranslation(width * index, 0);
        
        if ((index == 0 || index == 1 || index == 2) && self.flag == NO) {
            self.sg.selectedSegmentIndex = index;
        }
        
    } ConpleteBlock:^{
        self.flag = NO;
    }];
    
    /** 隐藏返回按钮 */
    self.backButton.hidden = YES;

}
#pragma mark ** segmentControll点击事件
- (void)segmentAction:(UISegmentedControl *)seg {
    self.flag = YES;
    [self.viewOfHome.collectionView setContentOffset:CGPointMake(self.view.frame.size.width * seg.selectedSegmentIndex, 0) animated:YES];
    

}




#pragma mark - 创建tabBarController
- (void)createTabBarView {
    TabBarView *view = [[TabBarView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50)];
    [self.view addSubview:view];
    [view release];
    /** 通过点击的下标控制显示的View */
    [view changeControllerViewWithTag:^(NSInteger tag) {
        switch (tag) {
            case 1:{
                [self.view bringSubviewToFront:self.viewOfHome];
                [self.view bringSubviewToFront:self.viewOfCamera];
                changeHome(self);
                break;
            }
            case 2:{
                [self.view bringSubviewToFront:self.viewOfShop];
                [self.view bringSubviewToFront:self.viewOfCamera];
                changeShop(self);
                break;
            }
            case 3:{
                [self.view bringSubviewToFront:self.viewOfServer];
                [self.view bringSubviewToFront:self.viewOfCamera];
                changeServer(self);
                break;
            }
            case 4:{
                [self.view bringSubviewToFront:self.viewOfMy];
                [self.view bringSubviewToFront:self.viewOfCamera];
                changeMy(self);
                break;
            }
        }
        
    }];
    /** 设置摄像机按钮 */
    self.viewOfCamera = [[CameraView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
    self.viewOfCamera.center = view.center;
    [self.view addSubview:self.viewOfCamera];
    [_viewOfCamera release];
    
}


- (void)dealloc {
    [_redLine release];
    [_viewOfMy release];
    [_viewOfShop release];
    [_viewOfServer release];
    [_viewOfMy release];
    [_sg release];
    [_viewOfCamera release];
    [super dealloc];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
